package DFS;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class Solution131 {


    public static void main(String[] args) {
        String s = "aab";
        System.out.println(partition(s));
    }

    public static List<List<String>> partition(String s) {
        List<List<String>> res = new ArrayList<>();
        List<String> chain = new ArrayList<>();

        dfs(s, 0, chain, res);

        return res;
    }

    public static void dfs(String s, int startIndex, List<String> chain, List<List<String>> res) {
        if (startIndex >= s.length()) {
            res.add(new ArrayList<>(chain));
            return;
        }
        for (int i = startIndex; i < s.length(); i++) {
            String substring = s.substring(startIndex, i + 1);
            if (isHuiWen(s, startIndex, i)) {
                chain.add(substring);
            } else {
                continue;
            }
            System.out.println("dfs前" + chain);
            dfs(s, i + 1, chain, res);
            System.out.println("dfs后" + chain);
            chain.remove(chain.size() - 1);
        }
    }

    //双指针法判断是否回文
    public static boolean isHuiWen(String s, Integer startIndex, Integer endIndex) {
        for (int i = startIndex, j = endIndex; i <= j; i++, j--) {
            if (s.charAt(i) != s.charAt(j)) {
                return false;
            }
        }
        return true;
    }


}
